/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 *
 * @author 10515
 */
public class StudentCourseOperation {
    
    public static Student readStuCourse(){
        Student stu = new Student(UserService.getCurrentUser());
            final String filePath = "./data/"+UserService.getCurrentUser()+".txt";
            try (FileInputStream fis = new FileInputStream(filePath);
                   InputStreamReader isr = new InputStreamReader(fis);
                   BufferedReader br = new BufferedReader(isr)) {
                   String line;
                   while ((line = br.readLine()) != null) {
                       String[] strs = line.split("\\s+");
                       stu.add(new Course(strs[0],strs[1],Integer.parseInt(strs[2]),Integer.parseInt(strs[3]),stu));
                   }
             } catch (FileNotFoundException e) {
                    e.printStackTrace();
             } catch (IOException e) {
                    e.printStackTrace();
            }
            return stu;
    }
    public static void writeStuCourse(String filePath,Student stu){
            try (OutputStream os = new FileOutputStream(filePath);
                 PrintWriter pw = new PrintWriter(os);) {
                for (int i = 0; i < stu.getElective().size(); i++) {
                    pw.print(stu.getElective().get(i).getName() +" "+ stu.getElective().get(i).getTeacher() + " "+stu.getElective().get(i).getNumberSelected()+" "+stu.getElective().get(i).getNumberOfStudents()+System.getProperty("line.separator"));
                }
             } catch (Exception e) {
                e.printStackTrace();
             }
    }
}
